/**
 * @author renbangjie renbangjie@126.com
 * @version V1.0
 */
package com.ai.service.reviewed.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ai.common.core.base.DTO;
import com.ai.common.core.mapper.Mapper;
import com.ai.common.core.pager.model.PagerUtil;
import com.ai.common.core.pager.service.IPagerService;
import com.ai.common.core.service.impl.BaseServiceImpl;
import com.ai.common.core.utils.CollectionUtils;
import com.ai.common.model.Pagination;
import com.ai.mmd.reviewed.dto.DlxxDTO;
import com.ai.mmd.reviewed.dto.QyshclDTO;
import com.ai.mmd.reviewed.dto.YhxxDTO;
import com.ai.mmd.reviewed.vo.DlxxVO;
import com.ai.mmd.reviewed.vo.YhxxVO;
import com.ai.service.reviewed.dao.DlxxModelMapper;
import com.ai.service.reviewed.dao.QyshclModelMapper;
import com.ai.service.reviewed.dao.YhxxModelMapper;
import com.ai.service.reviewed.model.DlxxModel;
import com.ai.service.reviewed.model.QyshclModel;
import com.ai.service.reviewed.model.YhxxModel;
import com.ai.service.reviewed.service.IYhxxService;
import com.ai.service.system.dao.GkinfoModelMapper;
import com.ai.service.system.dao.XxinfoModelMapper;

/**
 * 用户信息表的业务实现类
 *
 * @author renbangjie renbangjie@126.com
 */
@Service("iyhxxService")
public class YhxxServiceImpl extends BaseServiceImpl<YhxxModel>
        implements IYhxxService {


    /**
     * 详细信息的分页
     *
     * @param yhxxDTO
     * @return
     * @throws Exception
     */
    @Override
    public Pagination<YhxxVO> getListVO(YhxxDTO yhxxDTO) throws Exception {
        return PagerUtil.getPagination(new IPagerService<YhxxVO>() {
            @Override
            public int queryCount(DTO dto) throws Exception {
                return getMapper().getListCount(dto);
            }

            @Override
            public List<YhxxVO> queryList(DTO dto) throws Exception {
                List<YhxxVO> voList = new ArrayList<YhxxVO>();
                List<YhxxModel> yhxxList = getMapper().getList(dto);
                if (yhxxList.isEmpty()) {
                    return voList;
                }
                //会员登录信息
                DlxxDTO dlxxDTO = new DlxxDTO();
                List<String> yhidList = CollectionUtils
                        .getBeanFieldValues(yhxxList, "yhid");
                dlxxDTO.setYhidList(yhidList);
                Map<String, DlxxModel> dlxxMap = CollectionUtils
                        .toMap(dlxxModelMapper.getList(dlxxDTO), "yhid");

                YhxxVO yhxxVO = null;
                DlxxVO dlxxVO = null;

                for (YhxxModel yhxxModel : yhxxList) {
                    yhxxVO = new YhxxVO();
                    BeanUtils.copyProperties(yhxxModel, yhxxVO);
                    if (dlxxMap != null && !dlxxMap.isEmpty() && dlxxMap
                            .containsKey(yhxxVO.getYhid())) {
                        dlxxVO = new DlxxVO();
                        BeanUtils.copyProperties(dlxxMap.get(yhxxVO.getYhid()),
                                                 dlxxVO);
                        yhxxVO.setDlxxVO(dlxxVO);
                    }
                    voList.add(yhxxVO);
                }
                return voList;
            }
        }, yhxxDTO);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void insertSelective(YhxxModel yhxxModel,String yyzzfilename,String zzjgfilename,String swdjfilename,
			String qyzzfilename,String wtzmfilename,String szhyfilename)throws Exception {
    	yhxxModel.setCjsj(new Date());
		yhxxModelMapper.insert(yhxxModel);
		
		if (yyzzfilename!=null&&yyzzfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhxxModel.getYhid());
			qyshclModel.setShcl(yyzzfilename);
			qyshclModel.setCllx(1);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
		if (zzjgfilename!=null&&zzjgfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhxxModel.getYhid());
			qyshclModel.setShcl(zzjgfilename);
			qyshclModel.setCllx(2);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
		if (swdjfilename!=null&&swdjfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhxxModel.getYhid());
			qyshclModel.setShcl(swdjfilename);
			qyshclModel.setCllx(3);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
		if (qyzzfilename!=null&&qyzzfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhxxModel.getYhid());
			qyshclModel.setShcl(qyzzfilename);
			qyshclModel.setCllx(4);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
		if (wtzmfilename!=null&&wtzmfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhxxModel.getYhid());
			qyshclModel.setShcl(wtzmfilename);
			qyshclModel.setCllx(5);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
		if (szhyfilename!=null&&szhyfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhxxModel.getYhid());
			qyshclModel.setShcl(szhyfilename);
			qyshclModel.setCllx(6);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
	}
    
    @Transactional(rollbackFor = Exception.class)
    public void updateSelective(YhxxModel yhxxModel,String yyzzfilename,String zzjgfilename,String swdjfilename,
			String qyzzfilename,String wtzmfilename,String szhyfilename)throws Exception {
    	yhxxModel.setCjsj(new Date());
    	yhxxModel.setYhzt(0);
		yhxxModelMapper.updateByPrimaryKey(yhxxModel);
		
		if (yyzzfilename!=null&&yyzzfilename!="") {
			QyshclDTO qyshclDTO = new QyshclDTO();
			qyshclDTO.setYhid(yhxxModel.getYhid());
			qyshclDTO.setCllx(1);
			QyshclModel qyshclModel = qyshclModelMapper.getOne(qyshclDTO);
			if (qyshclModel!=null) {
				qyshclModel.setGxsj(new Date());
				qyshclModel.setShcl(yyzzfilename);
			}
			
			int nret = qyshclModelMapper.updateByPrimaryKey(qyshclModel);
			if (nret==0) {
				qyshclModelMapper.insert(qyshclModel);
			}
		}
		if (zzjgfilename!=null&&zzjgfilename!="") {
			QyshclDTO qyshclDTO = new QyshclDTO();
			qyshclDTO.setYhid(yhxxModel.getYhid());
			qyshclDTO.setCllx(2);
			QyshclModel qyshclModel = qyshclModelMapper.getOne(qyshclDTO);
			if (qyshclModel!=null) {
				qyshclModel.setGxsj(new Date());
				qyshclModel.setShcl(zzjgfilename);
			}
			
			int nret = qyshclModelMapper.updateByPrimaryKey(qyshclModel);
			if (nret==0) {
				qyshclModelMapper.insert(qyshclModel);
			}
		}
		if (swdjfilename!=null&&swdjfilename!="") {
			QyshclDTO qyshclDTO = new QyshclDTO();
			qyshclDTO.setYhid(yhxxModel.getYhid());
			qyshclDTO.setCllx(3);
			QyshclModel qyshclModel = qyshclModelMapper.getOne(qyshclDTO);
			if (qyshclModel!=null) {
				qyshclModel.setGxsj(new Date());
				qyshclModel.setShcl(swdjfilename);
			}
			
			int nret = qyshclModelMapper.updateByPrimaryKey(qyshclModel);
			if (nret==0) {
				qyshclModelMapper.insert(qyshclModel);
			}
		}
		if (qyzzfilename!=null&&qyzzfilename!="") {
			QyshclDTO qyshclDTO = new QyshclDTO();
			qyshclDTO.setYhid(yhxxModel.getYhid());
			qyshclDTO.setCllx(4);
			QyshclModel qyshclModel = qyshclModelMapper.getOne(qyshclDTO);
			if (qyshclModel!=null) {
				qyshclModel.setGxsj(new Date());
				qyshclModel.setShcl(qyzzfilename);
			}
			
			int nret = qyshclModelMapper.updateByPrimaryKey(qyshclModel);
			if (nret==0) {
				qyshclModelMapper.insert(qyshclModel);
			}
		}
		if (wtzmfilename!=null&&wtzmfilename!="") {
			QyshclDTO qyshclDTO = new QyshclDTO();
			qyshclDTO.setYhid(yhxxModel.getYhid());
			qyshclDTO.setCllx(5);
			QyshclModel qyshclModel = qyshclModelMapper.getOne(qyshclDTO);
			if (qyshclModel!=null) {
				qyshclModel.setGxsj(new Date());
				qyshclModel.setShcl(wtzmfilename);
			}
			
			int nret = qyshclModelMapper.updateByPrimaryKey(qyshclModel);
			if (nret==0) {
				qyshclModelMapper.insert(qyshclModel);
			}
		}
		if (szhyfilename!=null&&szhyfilename!="") {
			QyshclDTO qyshclDTO = new QyshclDTO();
			qyshclDTO.setYhid(yhxxModel.getYhid());
			qyshclDTO.setCllx(6);
			QyshclModel qyshclModel = qyshclModelMapper.getOne(qyshclDTO);
			if (qyshclModel!=null) {
				qyshclModel.setGxsj(new Date());
				qyshclModel.setShcl(szhyfilename);
			}
			
			int nret = qyshclModelMapper.updateByPrimaryKey(qyshclModel);
			if (nret==0) {
				qyshclModelMapper.insert(qyshclModel);
			}
		}
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertshcl(String yhid, String yyzzfilename,String zzjgfilename,String swdjfilename,
			String qyzzfilename,String wtzmfilename,String zjlx,String szhyfilename)throws Exception {
    	
    	if ("0".equals(zjlx)) {
			//三证合一
    		if (szhyfilename!=null&&szhyfilename!="") {
    			QyshclModel qyshclModel = new QyshclModel();
    			qyshclModel.setYhid(yhid);
    			qyshclModel.setShcl(szhyfilename);
    			qyshclModel.setCllx(6);
    			qyshclModel.setCjsj(new Date());
    			
    			qyshclModelMapper.insert(qyshclModel);
    		}
		}else {
			//老三证
			if (yyzzfilename!=null&&yyzzfilename!="") {
				QyshclModel qyshclModel = new QyshclModel();
				qyshclModel.setYhid(yhid);
				qyshclModel.setShcl(yyzzfilename);
				qyshclModel.setCllx(1);
				qyshclModel.setCjsj(new Date());
				
				qyshclModelMapper.insert(qyshclModel);
			}
			if (zzjgfilename!=null&&zzjgfilename!="") {
				QyshclModel qyshclModel = new QyshclModel();
				qyshclModel.setYhid(yhid);
				qyshclModel.setShcl(zzjgfilename);
				qyshclModel.setCllx(2);
				qyshclModel.setCjsj(new Date());
				
				qyshclModelMapper.insert(qyshclModel);
			}
			if (swdjfilename!=null&&swdjfilename!="") {
				QyshclModel qyshclModel = new QyshclModel();
				qyshclModel.setYhid(yhid);
				qyshclModel.setShcl(swdjfilename);
				qyshclModel.setCllx(3);
				qyshclModel.setCjsj(new Date());
				
				qyshclModelMapper.insert(qyshclModel);
			}
		}
				
		if (qyzzfilename!=null&&qyzzfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhid);
			qyshclModel.setShcl(qyzzfilename);
			qyshclModel.setCllx(4);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
		if (wtzmfilename!=null&&wtzmfilename!="") {
			QyshclModel qyshclModel = new QyshclModel();
			qyshclModel.setYhid(yhid);
			qyshclModel.setShcl(wtzmfilename);
			qyshclModel.setCllx(5);
			qyshclModel.setCjsj(new Date());
			
			qyshclModelMapper.insert(qyshclModel);
		}
	}
    
    

    @Resource(name = "yhxxModelMapper")
    private YhxxModelMapper yhxxModelMapper;

    @Resource(name = "qyshclModelMapper")
    private QyshclModelMapper qyshclModelMapper;

    @Resource(name = "dlxxModelMapper")
    private DlxxModelMapper dlxxModelMapper;
   
    @Resource(name = "gkinfoModelMapper")
	private GkinfoModelMapper gkinfoModelMapper;
    
    @Resource(name = "xxinfoModelMapper")
	private XxinfoModelMapper xxinfoModelMapper;

    @Override
    protected Mapper<YhxxModel> getMapper() {
        return yhxxModelMapper;
    }
}
